﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentData;
using Task.Util;

namespace Task.DB
{
    public class FluentDB
    {
        public static IDbContext ContextConfig()
        {
            return new DbContext().ConnectionStringName("DBContext", new SqlServerProvider());
        }

        public static IDbContext ContextSQL()
        {
            string configStr = MemoryCacheHelper.GetCacheItem("key_DataBaseConnectionString", delegate() { return ConfigEntity.DataBaseConnectionString(); }, new TimeSpan(0, 30, 0));//30分钟过期
            string dbType = MemoryCacheHelper.GetCacheItem("key_dbType", delegate()
            {
                INIFileUtil ini = new INIFileUtil(AppDomain.CurrentDomain.BaseDirectory + "/Configs/config.ini");
                return ini.IniReadValue("DataBase", "DBType");
            }, new TimeSpan(0, 30, 0));//30分钟过期
            DbProviderType dbTypeEnum = (DbProviderType)Enum.Parse(typeof(DbProviderType), dbType);

            if (dbTypeEnum == DbProviderType.SqlServer)
            {
                return new DbContext().ConnectionString(configStr, new SqlServerProvider());
            }
            else if (dbTypeEnum == DbProviderType.MySql)
            {
                return new DbContext().ConnectionString(configStr, new MySqlProvider());
            }
            return null;
        }
    }
}
